package com.ruoyi.order.controller;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.order.domain.SalesOrder;
import com.ruoyi.order.service.ISalesOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单管理Controller
 * 
 * @author lzp
 * @date 2024-12-29
 */
@RestController
@RequestMapping("/system/order")
public class SalesOrderController extends BaseController
{
    @Autowired
    private ISalesOrderService salesOrderService;

    /**
     * 查询订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(SalesOrder salesOrder)
    {
        startPage();
        List<SalesOrder> list = salesOrderService.selectSalesOrderList(salesOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:export')")
    @Log(title = "订单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SalesOrder salesOrder)
    {
        List<SalesOrder> list = salesOrderService.selectSalesOrderList(salesOrder);
        ExcelUtil<SalesOrder> util = new ExcelUtil<SalesOrder>(SalesOrder.class);
        util.exportExcel(response, list, "订单管理数据");
    }

    /**
     * 获取订单管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") String orderId)
    {
        return success(salesOrderService.selectSalesOrderByOrderId(orderId));
    }

    /**
     * 新增订单管理
     */
    @PreAuthorize("@ss.hasPermi('system:order:add')")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SalesOrder salesOrder)
    {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        salesOrder.setModifiedDate(now);
        salesOrder.setLastModifiedDate(now);
        salesOrder.setOrderDate(now.format(formatter));
        return toAjax(salesOrderService.insertSalesOrder(salesOrder));
    }

    /**
     * 修改订单管理
     */
    @PreAuthorize("@ss.hasPermi('system:order:edit')")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SalesOrder salesOrder)
    {
        LocalDateTime now = LocalDateTime.now();
        salesOrder.setModifiedDate(now);
        salesOrder.setLastModifiedDate(now);
        return toAjax(salesOrderService.updateSalesOrder(salesOrder));
    }

    /**
     * 删除订单管理
     */
    //@PreAuthorize("@ss.hasPermi('system:order:remove')")
    @Log(title = "订单管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable String[] orderIds)
    {
        return toAjax(salesOrderService.deleteSalesOrderByOrderIds(orderIds));
    }


    @GetMapping("/statusCount")
    public AjaxResult getOrderStatusCount()
    {
        List<Map<String, Object>> orderStatusCount = salesOrderService.selectOrderStatusCount();
        return AjaxResult.success(orderStatusCount);
    }

    @GetMapping("/CustomerStatusCount")
    public AjaxResult getCustomerStatusCount(@RequestParam String customerId)
    {
        List<Map<String, Object>> customerStatusCount = salesOrderService.selectCustomerOrderStatusCount(customerId);
        return AjaxResult.success(customerStatusCount);
    }


    @GetMapping("/dailyOrderCount")
    public AjaxResult getDailyOrderCount(@RequestParam String dateRange) {
        // 使用 Collections.singletonMap 创建不可变的 Map
        Map<String, String> params = Collections.singletonMap("dateRange", dateRange);
        List<Map<String, Object>> dailyOrderCount = salesOrderService.getDailyOrderCount(params);
        return AjaxResult.success(dailyOrderCount);
    }

   // @PreAuthorize("@ss.hasPermi('system:order:query')")
    @GetMapping("/todayTotalSales")
    public AjaxResult getTodayTotalSales()
    {
        Double totalSales = salesOrderService.selectTodayTotalSales();
        if(totalSales == null){
            totalSales =0.0;
        }
        return AjaxResult.success(totalSales);
    }

    /**
     * 查询本周的销售总额
     */
    //@PreAuthorize("@ss.hasPermi('system:order:query')")
    @GetMapping("/thisWeekTotalSales")
    public AjaxResult getThisWeekTotalSales()
    {
        Double totalSales = salesOrderService.selectThisWeekTotalSales();
        if(totalSales == null){
            totalSales =0.0;
        }
        return AjaxResult.success(totalSales);
    }

    /**
     * 查询本月的销售总额
     */
    //@PreAuthorize("@ss.hasPermi('system:order:query')")
    @GetMapping("/thisMonthTotalSales")
    public AjaxResult getThisMonthTotalSales()
    {
        Double totalSales = salesOrderService.selectThisMonthTotalSales();
        if(totalSales == null){
            totalSales =0.0;
        }
        return AjaxResult.success(totalSales);
    }

   // @PreAuthorize("@ss.hasPermi('system:order:query')")
    @GetMapping("/getTodayOrderCount")
    public AjaxResult getTodayOrderCount()
    {
        int  count = salesOrderService.selectTodayOrderCount();
        return AjaxResult.success(count);
    }

}
